package cn.edu.scut.sys.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.scut.sys.dao.DepartmentDao;
import cn.edu.scut.sys.service.DepartmentService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.vo.Department;

@Component("departmentService")
public class DepartmentServiceImpl implements DepartmentService{

private DepartmentDao departmentDao;
	
	@Resource(name="departmentDao")
	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	@Override
	public DataResponse<Department> list(DataRequest request) {
		DataResponse<Department> response = new DataResponse<Department>();  
        int count;//总记录数  
        int limit = request.getRows() <= 0 ? 20 : request.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = request.getPage() <= 0 ? 1 : request.getPage();//当前显示页码  
        List<Department> list;  
          
        count = departmentDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start;  
          
        list = departmentDao.list(start, limit, request.getSord(), request.getSidx());  
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;  
	}

	@Override
	public boolean add(Department department) {
		departmentDao.insert(department);
		return false;
	}
	
	@Override
	public Department getDepartment(String id){
		return departmentDao.getDepartment(id);
	}
	
	@Override
	public boolean delDepartment(int id){
		return departmentDao.delDepartment(id);
	}
	
	@Override
	public boolean updateDepartment(Department department) {
		return departmentDao.updateDepartment(department);
	}

	@Override
	public List<Department> listAll() {
		return departmentDao.listAll();
	}
}
